package com.eroad.product.tools;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.application.EmmApplication;
import com.eroad.product.tools.Time_PickerView.onSelectListener;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;

public class Time_SelectPicPopupWindow extends PopupWindow {

	private View mMenuView;
	private Time_PickerView Year, April, Day;
	private TextView order_yes, order_no;
	private String years_text, type_text, April_text;
	public List<String> Years;
	public List<String> Aprils;
	public List<String> Days;
	private int i = 0, m;
	private String[] ver;
	private RelativeLayout boss;
	private Button imageView1;
	private TextView textView1;

	public static boolean isYes = false, isNo = false;

	@SuppressWarnings("deprecation")
	public Time_SelectPicPopupWindow(final Activity context, OnClickListener clickListener, int number, Date date) {
		super(context);
		isYes = false;
		isNo = false;
		this.date = date;
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mMenuView = inflater.inflate(R.layout.time_home_dialog, null);
		Year = (Time_PickerView) mMenuView.findViewById(R.id.Year);
		April = (Time_PickerView) mMenuView.findViewById(R.id.April);
		Day = (Time_PickerView) mMenuView.findViewById(R.id.Day);
		textView1 = (TextView) mMenuView.findViewById(R.id.textView1);
		imageView1 = (Button) mMenuView.findViewById(R.id.imageView1);
		imageView1.setOnClickListener(clickListener);

		if (EmmApplication.monthCode == number) {
			Day.setVisibility(View.GONE);
		} else {
			Day.setVisibility(View.VISIBLE);
		}

		Picker();
		// 设置按钮监听
		// 设置SelectPicPopupWindow的View
		this.setContentView(mMenuView);
		// 设置SelectPicPopupWindow弹出窗体的宽
		this.setWidth(LayoutParams.FILL_PARENT);
		// 设置SelectPicPopupWindow弹出窗体的高
		this.setHeight(LayoutParams.WRAP_CONTENT);
		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(true);
		// 设置SelectPicPopupWindow弹出窗体动画效果
		// this.setAnimationStyle(R.style.AnimBottom);
		// 实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(Color.parseColor("#50000000"));
		// 设置SelectPicPopupWindow弹出窗体的背景
		this.setBackgroundDrawable(dw);
		// mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
		mMenuView.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				int height = mMenuView.findViewById(R.id.yes).getTop();
				int y = (int) event.getY();
				if (event.getAction() == MotionEvent.ACTION_UP) {
					if (y < height) {
						dismiss();
						Aprils.clear();
						Years.clear();
						Days.clear();
					}
				}
				return true;
			}
		});

	}

	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {

			if (msg.what == 1) {
				SimpleDateFormat formats = new SimpleDateFormat("yyyyMMdd");
				Calendar c = Calendar.getInstance();
				try {
					c.setTime(formats.parse(getLanguage() + getType_One() + getType()));
				} catch (ParseException e) {
					e.printStackTrace();
				}
				Date endDate = c.getTime();
				String str = formats.format(endDate);
			}

		};
	};
	Date date;
	int ri;

	// 填充数据
	public void Picker() {
		Years = new ArrayList<String>();
		Aprils = new ArrayList<String>();
		Days = new ArrayList<String>();

		SimpleDateFormat format = new SimpleDateFormat("yyyy");
		SimpleDateFormat format_1 = new SimpleDateFormat("MM");
		SimpleDateFormat format_2 = new SimpleDateFormat("dddd");
		Date date2 = new Date(System.currentTimeMillis());

		int nian = 1;
		System.out.println("aaaaaaaa" + Integer.parseInt(format.format(date)));
		for (int i = Integer.parseInt(format.format(date2)) - 100; i <= (Integer.parseInt(format.format(date)) + 30); i++) {
			Years.add(i + "年");
			if (Integer.valueOf(EmmApplication.dataTime(date.getTime(), "yyyy")) == i) {
				nian = i - (Integer.parseInt(format.format(date2)) - 100);
			}
		}
		int yue = 1;
		for (int i = 1; i <= 12; i++) {
			Aprils.add(i + "月");
			// Log.v("TAG0:", i + "月");
			if (Integer.valueOf(EmmApplication.dataTime(date.getTime(), "MM")) == i) {
				yue = i - 1;
			}
		}
		ri = 1;
		for (int i = 1; i <= EmmApplication.getDaysByYearMonth(Integer.valueOf(format.format(date)).intValue(), Integer
				.valueOf(format_1.format(date)).intValue()); i++) {
			Days.add(i + "日");
			// Log.v("TAG1:", "" + EmmApplication.dataTime(EmmApplication.geTime(),
			// "dd日"));
			// Log.v("TAG2:", "" + ((i < 10 ? "0" + i : i) + "日"));
			if (EmmApplication.dataTime(date.getTime(), "dd日").equals(((i < 10 ? "0" + i : i) + "日"))) {
				ri = i - 1;
			}
		}
		Year.setData(Years, 1, 0);
		April.setData(Aprils, 2, 1);
		Day.setData(Days, 2, 2);
		years_text = EmmApplication.dataTime(date.getTime(), "yyyy年");
		type_text = EmmApplication.dataTime(date.getTime(), "dd日");
		April_text = EmmApplication.dataTime(date.getTime(), "MM月");
		Log.w("初始时间：", years_text + "" + April_text + "" + type_text + "");
		String str = years_text;
		String split = "年";
		StringTokenizer token = new StringTokenizer(str, split);
		ver = new String[token.countTokens()];
		while (token.hasMoreTokens()) {
			ver[i] = token.nextToken();
			System.out.println(ver[i]);
			i++;
		}
		m = Integer.parseInt(ver[0]);
		Year.setOnSelectListener(new onSelectListener() {
			@Override
			public void onSelect(String text) {
				i = 0;
				System.out.println(text + "[[");
				if (ver != null) {
					ver = null;
				}
				years_text = text;
				String str = years_text;
				String split = "年";
				StringTokenizer token = new StringTokenizer(str, split);
				ver = new String[token.countTokens()];
				while (token.hasMoreTokens()) {
					ver[i] = token.nextToken();
					System.out.println(ver[i]);
					i++;
				}
				m = Integer.parseInt(ver[0]);
				Days.clear();
				if (April_text.equals("1月") || April_text.equals("3月") || April_text.equals("5月")
						|| April_text.equals("7月") || April_text.equals("8月") || April_text.equals("10月")
						|| April_text.equals("12月")) {
					for (int i = 1; i <= 31; i++) {
						Days.add(i + "日");
						if (EmmApplication.dataTime(date.getTime(), "dd日").equals(((i < 10 ? "0" + i : i) + "日"))) {
							ri = i - 1;
						}
					}
				} else if (April_text.equals("2月")) {
					if (m % 4 == 0 && m % 100 != 0 || m % 400 == 0) {
						if (April_text.equals("2月")) {
							for (int i = 1; i <= 29; i++) {
								Days.add(i + "日");
								if (EmmApplication.dataTime(date.getTime(), "dd日").equals(((i < 10 ? "0" + i : i) + "日"))) {
									ri = i - 1;
								}
							}
						}
					} else {
						for (int i = 1; i <= 28; i++) {
							Days.add(i + "日");
							if (EmmApplication.dataTime(date.getTime(), "dd日").equals(((i < 10 ? "0" + i : i) + "日"))) {
								ri = i - 1;
							}
						}
					}
				} else {
					for (int i = 1; i <= 30; i++) {
						Days.add(i + "日");
						if (EmmApplication.dataTime(date.getTime(), "dd日").equals(((i < 10 ? "0" + i : i) + "日"))) {
							ri = i - 1;
						}
					}
				}
				Day.setData(Days, 2, 2);
				Day.setSelected(ri);
			}
		});
		Day.setOnSelectListener(new onSelectListener() {
			@Override
			public void onSelect(String text) {
				System.out.println(text + "]]");
				type_text = text;
			}
		});

		April.setOnSelectListener(new onSelectListener() {
			@Override
			public void onSelect(String text) {
				System.out.println(text + "]]");
				April_text = text;
				Days.clear();
				if (text.equals("1月") || text.equals("3月") || text.equals("5月") || text.equals("7月")
						|| text.equals("8月") || text.equals("10月") || text.equals("12月")) {

					for (int i = 1; i <= 31; i++) {
						Days.add(i + "日");
						if (EmmApplication.dataTime(date.getTime(), "dd日").equals(((i < 10 ? "0" + i : i) + "日"))) {
							ri = i - 1;
						}
					}
				} else if (text.equals("2月")) {

					if (m % 4 == 0 && m % 100 != 0 || m % 400 == 0) {
						for (int i = 1; i <= 29; i++) {
							Days.add(i + "日");
							if (EmmApplication.dataTime(date.getTime(), "dd日").equals(((i < 10 ? "0" + i : i) + "日"))) {
								ri = i - 1;
							}
						}
					} else {
						for (int i = 1; i <= 28; i++) {
							Days.add(i + "日");
							if (EmmApplication.dataTime(date.getTime(), "dd日").equals(((i < 10 ? "0" + i : i) + "日"))) {
								ri = i - 1;
							}
						}
					}
				} else {

					for (int i = 1; i <= 30; i++) {
						Days.add(i + "日");
						if (EmmApplication.dataTime(date.getTime(), "dd日").equals(((i < 10 ? "0" + i : i) + "日"))) {
							ri = i - 1;
						}
					}
				}
				Day.setData(Days, 2, 2);
				Day.setSelected(ri);
			}
		});
		Year.setSelected(nian);
		April.setSelected(yue);
		Day.setSelected(ri);
	}

	public String getLanguage() {
		return years_text;
	}

	public String getType() {
		return type_text;
	}

	public String getType_One() {

		return April_text;
	}
}
